package org.yang.faq.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.yang.faq.model.Comment;
import org.yang.faq.model.Program;
import org.yang.faq.service.CommentService;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api
@Controller
@RequestMapping("/CommentController")
public class CommentController {
	@Autowired
	private CommentService commentService;

	@ApiOperation(value = "评论查询", notes = "根据节目pId获取对应的评论")
	@RequestMapping(value = "/Comment", method = RequestMethod.GET)
	@ResponseBody
	public Page<Comment> findAll(@RequestParam(value = "pId", required = false) String pId) {
		Page<Comment> page = new Page<Comment>(1, 100);
		EntityWrapper wrapper = new EntityWrapper<Comment>();
		if (pId == null) {
			wrapper.orderBy("create_time");
		} else {
			wrapper.eq("p_id", pId).orderBy("create_time");
		}

		page = commentService.selectPage(page, wrapper);

		return page;
	}

	@ApiOperation(value = "评论添加", notes = "评论节目")
	@RequestMapping(value = "/Comment", method = RequestMethod.POST)
	@ResponseBody
	public String addComment(@RequestParam(value = "id", required = false) String id,
			@RequestParam(value = "pId", required = false) String pId,
			@RequestParam(value = "wxId", required = false) String wxId,
			@RequestParam(value = "wxNickname", required = false) String wxNickname,
			@RequestParam(value = "content", required = false) String content) {
		
		
		Comment comment = new Comment();
		comment.setpId(pId);
		comment.setWxId(wxId);
		comment.setWxNickname(wxNickname);
		comment.setContent(content);
		comment.setCreateTime(new Date());
		
		return commentService.doComment(comment);

	}
}
